package com.lxh.lxhcrm.model.service;

import com.lxh.lxhcrm.model.dao.customer.CusDao;
import com.lxh.lxhcrm.model.dao.preSale.DetailOfferDao;
import com.lxh.lxhcrm.model.dao.preSale.OppoExamDao;
import com.lxh.lxhcrm.model.dao.preSale.OppoStateDao;
import com.lxh.lxhcrm.model.dao.preSale.OpportunityDao;
import com.lxh.lxhcrm.model.mappers.emp.EmpMapper;
import com.lxh.lxhcrm.model.mappers.preSale.OppoMapper;
import com.lxh.lxhcrm.model.pojos.*;
import com.lxh.lxhcrm.model.vo.CustomerVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 开发团队 ：lxh
 * 开发人员 ：罗晓
 * 开发时间 ：2020/1/9 10:35
 * 类的名称 ：PreSaleService
 * 类的描述 ：售前模块销售机会的service类，所有业务逻辑都在此
 */
@Service
public class PreSaleService {
    @Resource
    OppoMapper om;
    @Resource
    OpportunityDao od;
    @Resource
    OppoStateDao os;
    @Resource
    DetailOfferDao dod;
    @Resource
    CustomerService cs;
    @Resource
    CusDao cdo;
    @Resource
    OppoExamDao oed;
    @Resource
    EmpMapper ep;

    /**
     * 获取所有的销售机会
     * @return List<Map<String,Object>>
     */
    public List<Opportunity> findAllOppo(Integer state,String search,Integer searchBy,Integer customerId){
        return om.findAllOppo(state,search,searchBy,customerId);
    }


    public List<OppoState> getOppoState(){
        return os.findAll();
    }

    /**
     * 销售机会添加或修改方法
     *
     */
    public void saveOpportunity(Opportunity opportunity){
        //执行前先获取老的合同对象
        Opportunity oldOppo = null;
        List<Integer> listId = new ArrayList();
        //先保存机会
        Opportunity oppo = od.save(opportunity);
        if(opportunity.getOppoId() != 0){
            oldOppo = od.findById(opportunity.getOppoId()).orElse(null);
            // 获取销售机会中 产品的编号列表
            oldOppo.getOppoProList().forEach(oppoProList -> {
                //将全部产品放入列表
                listId.add(oppoProList.getDetailOfferId());
            });
        }
        opportunity.getOppoProList().forEach(op ->{
            // 比对以前的机会中现在没有的：有的就剔除，剩下的就是没有的了
            if (listId.indexOf(op.getDetailOfferId())>=0){
                listId.remove(listId.indexOf(op.getDetailOfferId()));
            }
            op.setOpportunity(oppo);
            dod.save(op);
        });
        // 删除已经不存在的产品
        for (Integer id: listId){
            dod.deleteById(id);
        }
        //修改客户状态
        cs.stageChange2(opportunity.getCustomer());
    }

    /**
     * 判断此客户是否已经是机会客户或者合同客户
     * @param customerId
     * @return
     */

    public Integer judgeIsOppo(Integer customerId){
        CustomerVO customer = cdo.findByCustomerId(customerId);
        int stateId = customer.getStageId();
        return stateId;
    }

    /**
     * 只是根据customerIdl来查opportunity
     * @param state
     * @param search
     * @param customerId
     * @return
     */
    public List<Opportunity> findOppoByCus(Integer state,String search,Integer searchBy,Integer customerId) {
        return om.findAllOppo(state,search,searchBy,customerId);
    }

    /**
     * 根据oppo_id查此机会的各次评定
     * @param oppoId
     * @return
     */
    public List<OppoExam> findOppoExam(Integer oppoId){
        return om.findOppoExam(oppoId);
    }


    public void saveOppoExam(OppoExam oppoExam) {
        oed.save(oppoExam);
    }

    /**
     * 根据empId，oppoStateId，date来查询detail_offer里的报价产品
     * @param empId
     * @param oppoStateId
     * @param date
     * @return
     */
    public List<Map<String,Object>> findNeedPro(Integer empId , Integer oppoStateId,Date date ){
        return om.findNeedPro(empId, oppoStateId,date);
    }

    /**
     * 根据pro_detail_id,来查找有此类商品的销售机会
     * @param proDetailId
     * @return
     */
    public List<Map<String,Object>> findDetailPro(Integer proDetailId, Date date){
        return om.findDetailPro(proDetailId,date);
    }

    /**
     * 简单的查询Emp
     * @return
     */
    public List<Map<String,Object>> findEasyEmp(){
        return om.findEasyEmp();
    }

    /**
     * 查询此用户是否具备审核功能
     * @param empId
     * @return
     */
    public boolean findDuty(Integer empId){
        boolean index = false;
        int item = ep.findDuty(empId);
        if(item == 1 || item == 2){
            index = true;
        }
        return index;
    }

    /**
     * 根据oppoId来灭活
     * @param oppoId
     */
    public void deleteOppo(Integer oppoId) {
        om.lossActivity(oppoId);
    }

    /**
     * 简单的机会查询
     * @return
     */
    public List<Map<String,Object>> selectOppo(){
        return om.selectOppo();
    }

}
